/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.javaeye.delivery.dto.CustomerOrder;
import com.javaeye.delivery.dto.OrderDetail;
import com.javaeye.delivery.dto.OrderDetailBatchInfo;
import com.javaeye.store.dto.MaterialsBatchDetail;
import com.javaeye.store.service.IMaterialsBatchInfoService;

public class PrepareOutStroeAction extends OrderAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private IMaterialsBatchInfoService batchInfoService;
	
	private List<Float> outStroeNumbers;
	
	private List<String> outStroeBatchNos;
	
	public void setBatchInfoService(IMaterialsBatchInfoService batchInfoService) {
		this.batchInfoService = batchInfoService;
	}

	/**
	 * 查询需要整理发货的出货单列表
	 * @return
	 * @throws Exception
	 */
	public String queryPrepareOutStroeList() throws Exception {
		getCondition().setStatus(new Integer[]{CustomerOrder.ORDER_STATES_BUSINESS});
		return queryOrderList();
	}
	
	/**
	 * 查询用于整理发货
	 * @return
	 * @throws Exception
	 */
	public String queryOrderPrepareOutStroe() throws Exception {
		queryOrderBaseInfo();
		CustomerOrder order = getOrder();
		Set<OrderDetail> details = order.getOrderDetails();
		for (OrderDetail detail : details) {
			List<MaterialsBatchDetail> batchInfos = 
				batchInfoService.getBatchInfoByMaterialsId(detail.getProductId());
			
			float number = detail.getNumber();
			List<MaterialsBatchDetail> computerInfos = new ArrayList<MaterialsBatchDetail>();
			for (MaterialsBatchDetail info : batchInfos) {
				if (number > 0) {
					float storeNumber = info.getNumber() - info.getOutNumber();
					float outNumber = (storeNumber < number) ? storeNumber : number;
					number -= outNumber;
					MaterialsBatchDetail batchInfo = new MaterialsBatchDetail();
					batchInfo.setBatchNo(info.getBatchNo());
					batchInfo.setNumber(storeNumber);
					batchInfo.setOutNumber(outNumber);
					computerInfos.add(batchInfo);
				}
			}
			detail.setBatchInfos(computerInfos);
		}
		return SUCCESS;
	}
	
	public String saveOrderPrepareOutStroeResult() throws Exception {
		//理货批次发货信息
		List<OrderDetailBatchInfo> batchInfos = new ArrayList<OrderDetailBatchInfo>();
		for (int i = 0; i < outStroeNumbers.size(); i++) {
			float number = outStroeNumbers.get(i);
			if (number > 0) {
				OrderDetailBatchInfo orderDetailBatchInfo = new OrderDetailBatchInfo();
				orderDetailBatchInfo.setOrderId(getOrderId());
				orderDetailBatchInfo.setOrderDetailId(getDetailIds().get(i));
				orderDetailBatchInfo.setProductId(getProductIds().get(i));
				orderDetailBatchInfo.setBatchNo(outStroeBatchNos.get(i));
				orderDetailBatchInfo.setNumber(number);
				batchInfos.add(orderDetailBatchInfo);
			}
		}
		//计算总的发货数量
		CustomerOrder order = getService().getOrderBaseInfo(getOrderId());
		Set<OrderDetail> details = order.getOrderDetails();
		for (OrderDetail detail : details) {
			int totleOutNumber = getTotleOutNumber(detail.getId(), batchInfos);
			detail.setOutStoreNumber(totleOutNumber);
		}
		getService().saveOrder(order, batchInfos);
		return SUCCESS;
	}
	
	private int getTotleOutNumber(int detailId, List<OrderDetailBatchInfo> batchInfos) {
		int totleNumber = 0;
		for (OrderDetailBatchInfo batchInfo : batchInfos) {
			if (batchInfo.getOrderDetailId() == detailId) {
				totleNumber += batchInfo.getNumber();
			}
		}
		return totleNumber;
	}

	public List<String> getOutStroeBatchNos() {
		return outStroeBatchNos;
	}

	public void setOutStroeBatchNos(List<String> outStroeBatchNos) {
		this.outStroeBatchNos = outStroeBatchNos;
	}

	public void setOutStroeNumbers(List<Float> outStroeNumbers) {
		this.outStroeNumbers = outStroeNumbers;
	}

	public List<Float> getOutStroeNumbers() {
		return outStroeNumbers;
	}
	
}
